//
//  DestinstionDetailsViewController.m
//  Journey
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DestinstionDetailsViewController.h"
//#import "DestinstionScrollView.h"
#import "SSNetworking.h"
#import "destinstionDetailModel.h"
#import "discountDetailsTableViewCell.h"
#import "discountTableViewCell.h"
#import "detailsModel.h"
#import "discountModel.h"
#import "ScrollView.h"
#import "YNDataReading.h"
static NSString *const detailsCell = @"DetailsCELL";
static NSString *const discountCell = @"DiscountCELL";
@interface DestinstionDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *detailsTableView;
@property(nonatomic,strong)UIView *HeaderView;
@property(nonatomic,strong)NSMutableArray *photoArray;
@property(nonatomic,strong)NSMutableArray *AllArray;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)ScrollView *destinstionSV;


@end

@implementation DestinstionDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  //  self.view.backgroundColor = [UIColor whiteColor];
    
    //初始化数组
    self.photoArray = [NSMutableArray array];
    self.AllArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    
    self.detailsTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.detailsTableView];
    self.detailsTableView.delegate = self;
    self.detailsTableView.dataSource = self;
    //注册
    [self.detailsTableView registerClass:[discountDetailsTableViewCell class] forCellReuseIdentifier:detailsCell];
   [self.detailsTableView registerClass:[discountTableViewCell class] forCellReuseIdentifier:discountCell];
    self.detailsTableView.showsVerticalScrollIndicator = NO;
    
    //加载条
    //显示
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self getData];
    //隐藏
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}



-(void)getData{
  
    
    
    
     NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=867323023990045&track_app_version=6.7.1&track_app_channel=xiaomi&track_device_info=cancro&track_os=Android4.4.4&app_installtime=1443411950696&lat=38.883818&lon=121.544801&country_id=%@",self.string];

   //  NSLog(@"%@",self.string);
        [SSNetworking ssGETWithURL:url parameter:nil success:^(id result) {
            [[YNDataReading shareYNataReading] writeCachePlist:@"desList" result:result];
            
    NSDictionary *dic = [[YNDataReading shareYNataReading] readCachePlist:@"desList"];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            destinstionDetailModel *destinstion = [[destinstionDetailModel alloc] init];
            
            [destinstion setValuesForKeysWithDictionary:dataDic];
            [self.titleArray addObject:destinstion];
            if (destinstion.discount.count == 0) {
                [self.AllArray addObject:destinstion.hot_city];
            }else{
                [self.AllArray addObject:destinstion.hot_city];
                
                [self.AllArray addObject:destinstion.discount];
            }
            
            // NSLog(@"%@",destinstion.hot_city);
            
            self.photoArray = destinstion.photos;
            //添加头视图
            self.HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 * WIDTHC)];
            
            self.detailsTableView.tableHeaderView = self.HeaderView;
            
            [self getScrollViewDataWith:[NSString stringWithFormat:@"%@\n%@\n%@",destinstion.cnname,destinstion.enname,destinstion.entryCont]];
            
            [self.detailsTableView reloadData];
            
            
        } failure:^(NSError *error) {
            
            
        } network:^(BOOL networking) {
            
            NSDictionary *dic = [[YNDataReading shareYNataReading] readCachePlist:@"desList"];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            
            destinstionDetailModel *destinstion = [[destinstionDetailModel alloc] init];
            [destinstion setValuesForKeysWithDictionary:dataDic];
            [self.titleArray addObject:destinstion];

            
            if (destinstion.discount.count == 0) {
                [self.AllArray addObject:destinstion.hot_city];
            }else{
                [self.AllArray addObject:destinstion.hot_city];

                 [self.AllArray addObject:destinstion.discount];
            }
            
        
            // NSLog(@"%@",destinstion.hot_city);
            
            self.photoArray = destinstion.photos;
            //添加头视图
            self.HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 * WIDTHC)];
            
            self.detailsTableView.tableHeaderView = self.HeaderView;
            
            [self getScrollViewDataWith:[NSString stringWithFormat:@"%@\n%@\n%@",destinstion.cnname,destinstion.enname,destinstion.entryCont]];
            
          
            
            [self.detailsTableView reloadData];

            
        }];
    
   }



//轮播图方法(需要加轮播图文字)
-(void)getScrollViewDataWith:(NSString *)title{
    
 //   NSLog(@"%@",title);
    ScrollView *destinstionSV = [[ScrollView alloc] initWithFrame:self.HeaderView.frame];
  [destinstionSV setImages: self.photoArray];
    self.destinstionSV = destinstionSV;
    
    [self.HeaderView addSubview:destinstionSV];
    UIView *view = [[UIView alloc] initWithFrame:self.HeaderView.frame];
    view.backgroundColor = [UIColor clearColor];
    [destinstionSV addSubview:view];
    
    [self getButtonWith:title];
   
     
}


//改变分区颜色
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    // Background color
    
    view.dk_tintColorPicker = DKColorPickerWithRGB(0xF5F5F5,0x363636);
    // Text Color
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    
    [header.textLabel setTextColor:[UIColor blackColor]];
    
}





-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return @"城市";
    }else{
        
    return @"超值自由行";
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    
    return self.AllArray.count;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 250 * WIDTHC;
    }
    return 100 * WIDTHC;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    
    NSArray *tempArray = [self.AllArray objectAtIndex:section];
  
    return tempArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
         discountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:discountCell];
        cell.tempArray = [self.AllArray objectAtIndex:indexPath.section];
        
    return cell;
        
    }
    
    discountDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailsCell];
   NSArray *tempArray = [self.AllArray objectAtIndex:indexPath.section];
    DiscountModel *discount = [tempArray objectAtIndex:indexPath.row];
    cell.dismodel = discount;
    return cell;
}


//头视图上的button
-(void)getButtonWith:(NSString *)title{
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    returnButton.frame = CGRectMake(10 * WIDTHC, 10 * WIDTHC, 50 * WIDTHC, 50 * WIDTHC);
    [self.destinstionSV addSubview:returnButton];
    [returnButton addTarget:self action:@selector(leftButtonAction:) forControlEvents: UIControlEventTouchUpInside];
 
    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    titleButton.backgroundColor =[UIColor blackColor];
    titleButton.alpha = 0.5;
    
    [titleButton setTitle:title forState:UIControlStateNormal];
    titleButton.titleLabel.numberOfLines = 4;
    titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    titleButton.frame = CGRectMake(0, 200 * WIDTHC, self.destinstionSV.frame.size.width, 100 * WIDTHC);
  
    [self.destinstionSV addSubview:titleButton];
    
    
    
}







//返回执行方法
-(void)leftButtonAction:(UIButton *)item{
    
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}


@end
